package com.qyhl.webtv.commonlib.utils.view.editbar;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.Nullable;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.R;
import com.qyhl.webtv.basiclib.utils.ContextUtilts;


/**
 * Created by Administrator on 2018/3/14.
 */

public class EditBar extends LinearLayout implements View.OnClickListener {
    public static final int TYPE_DEFAULT = 0;
    public static final int TYPE_SEND = 1;
    private int type = TYPE_DEFAULT;

    private EditText contented;
    private RippleImageView imgCollect;
    private RippleImageView imgShare;
    private RippleImageView imgLike;
    private RippleImageView imgPraise;
    private TextView content_text;
    private RippleImageView commentNum;
    private RippleTextView send_text;
    private TextView shop_btn;
    private LinearLayout comment_layout;
    private RelativeLayout content_layout;

    private boolean isCollect = false;
    private EditBarOnClickListenerImpl editBarOnClickListener;
    private LinearLayout rootLayout;
    private Activity activity;
    private boolean isHideComment = false;
    private boolean isHidePraise = false;
    private boolean isHideShare = false;
    private boolean isHideCollect = false;
    private boolean isShowShop = false;
    private boolean isShowLike = false;
    private boolean isCommentLayoutHide = false;
    private boolean isHideText = false;

    private boolean hasEdit = true;

    private boolean isPraised;

    private Context mContext;

    private MBadgeView badgeView;

    private MBadgeView commentBadgeView;

    private MBadgeView praiseBadgeView;

    private MBadgeView collectBadgeView;

    private MBadgeView shareBadgeView;

    public EditBar(Context context) {
        this(context, null);
    }

    public EditBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EditBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initview();
    }

    private void initview() {
        LayoutInflater.from(getContext()).inflate(R.layout.editbar_view, this, true);
        contented = (EditText) findViewById(R.id.contented);
        imgCollect = (RippleImageView) findViewById(R.id.imgCollect);
        imgShare = (RippleImageView) findViewById(R.id.imgShare);
        commentNum = (RippleImageView) findViewById(R.id.commentNum);
        imgPraise = (RippleImageView) findViewById(R.id.imgPraise);
        send_text = (RippleTextView) findViewById(R.id.send_text);
        content_text = (TextView) findViewById(R.id.content_text);
        content_layout = (RelativeLayout) findViewById(R.id.content_layout);
        shop_btn = (TextView) findViewById(R.id.shop_btn);
        imgLike = (RippleImageView) findViewById(R.id.imglike);
        comment_layout = findViewById(R.id.comment_layout);
        rootLayout = (LinearLayout) findViewById(R.id.editbar_background);
        rootLayout.setBackgroundResource(R.color.white);


        content_text.setOnClickListener(this);
        imgShare.setOnClickListener(this);
        imgLike.setOnClickListener(this);
        commentNum.setOnClickListener(this);
        send_text.setOnClickListener(this);
        imgCollect.setOnClickListener(this);
        contented.setOnClickListener(this);
        shop_btn.setOnClickListener(this);
        imgPraise.setOnClickListener(this);

        contented.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                content_text.setText(contented.getText().toString());
            }
        });
    }

    public void setEditBarOnClickListener(EditBarOnClickListenerImpl editBarOnClickListener) {
        this.editBarOnClickListener = editBarOnClickListener;
    }

    @Override
    public void onClick(View v) {
        if (editBarOnClickListener == null) {
            return;
        }
        int i = v.getId();
        if (i == R.id.imgCollect) {
            setCollect(!isCollect);
            editBarOnClickListener.onCollect(isCollect);

        } else if (i == R.id.imgShare) {
            editBarOnClickListener.onShare();

        } else if (i == R.id.commentNum) {
            editBarOnClickListener.onComment();

        } else if (i == R.id.send_text) {
            editBarOnClickListener.onSend();

        } else if (i == R.id.content_text) {
            if (hasEdit) {
                changeStateSend(activity);
            } else {
                editBarOnClickListener.onComment();
            }

        } else if (i == R.id.contented) {
            editBarOnClickListener.editClick();

        } else if (i == R.id.shop_btn) {
            editBarOnClickListener.shopList();
        } else if (i == R.id.imglike) {
            editBarOnClickListener.doFollow();
        } else if (i == R.id.imgPraise) {
            editBarOnClickListener.doPraise(isPraised);
        }
    }

    /**
     * 默认状态
     */
    public void changeStateDefault(Activity activity) {
        if (type != TYPE_DEFAULT) {
            type = TYPE_DEFAULT;
            content_text.setVisibility(VISIBLE);
            if (isHideCollect) {
                imgCollect.setVisibility(GONE);
            } else {
                imgCollect.setVisibility(VISIBLE);
                if (collectBadgeView != null) {
                    collectBadgeView.setVisibility(VISIBLE);
                }
            }
            if (isHideShare) {
                imgShare.setVisibility(GONE);
            } else {
                imgShare.setVisibility(VISIBLE);
                if (shareBadgeView != null) {
                    shareBadgeView.setVisibility(VISIBLE);
                }
            }
            if (isHideComment) {
                commentNum.setVisibility(GONE);
            } else {
                commentNum.setVisibility(VISIBLE);
                if (commentBadgeView != null) {
                    commentBadgeView.setVisibility(VISIBLE);
                }
            }

            if (isHideText) {
                content_text.setVisibility(INVISIBLE);
            } else {
                content_text.setVisibility(VISIBLE);
            }

            if (isCommentLayoutHide) {
                comment_layout.setVisibility(INVISIBLE);
            } else {
                comment_layout.setVisibility(VISIBLE);
            }

            if (isShowShop) {
                shop_btn.setVisibility(VISIBLE);
            } else {
                shop_btn.setVisibility(GONE);
            }

            if (isShowLike) {
                imgLike.setVisibility(VISIBLE);
                if (badgeView != null) {
                    badgeView.setVisibility(VISIBLE);
                }
            } else {
                imgLike.setVisibility(GONE);
            }

            if (isHidePraise) {
                imgPraise.setVisibility(GONE);
            } else {
                imgPraise.setVisibility(VISIBLE);
                if (praiseBadgeView != null) {
                    praiseBadgeView.setVisibility(VISIBLE);
                }
            }

            content_layout.setVisibility(GONE);

            send_text.setVisibility(GONE);
            contented.setVisibility(GONE);
        }
    }

    /**
     * 设置发送字体颜色
     *
     * @param colorId
     * @return
     */
    public EditBar setSendTextColor(int colorId) {
        send_text.setTextColor(colorId);
        return this;
    }

    /**
     * 输入状态
     */
    public void changeStateSend(Activity activity) {
        if (type != TYPE_SEND) {
            type = TYPE_SEND;
            content_text.setVisibility(GONE);
            imgCollect.setVisibility(GONE);
            imgShare.setVisibility(GONE);
            imgLike.setVisibility(GONE);
            if (badgeView != null) {
                badgeView.setVisibility(GONE);
            }
            if (collectBadgeView != null) {
                collectBadgeView.setVisibility(GONE);
            }

            if (praiseBadgeView != null) {
                praiseBadgeView.setVisibility(GONE);
            }

            if (commentBadgeView != null) {
                commentBadgeView.setVisibility(GONE);
            }

            if (shareBadgeView != null) {
                shareBadgeView.setVisibility(GONE);
            }
            imgPraise.setVisibility(GONE);
            commentNum.setVisibility(GONE);
            shop_btn.setVisibility(GONE);

            send_text.setVisibility(VISIBLE);
            contented.setVisibility(VISIBLE);
        }
        content_layout.setVisibility(VISIBLE);
        contented.setFocusable(true);
        contented.setFocusableInTouchMode(true);
        contented.requestFocus();
        InputMethodManager imm;
        if (activity != null) {
            imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        } else {
            imm = (InputMethodManager) ContextUtilts.getInstance().getmContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        }
        imm.showSoftInput(contented, InputMethodManager.SHOW_IMPLICIT);
    }

    public void closeKey(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(contented.getWindowToken(), 0);
    }

    /**
     * 是否需要编辑框
     *
     * @param b
     * @return
     */
    public EditBar isHasEdit(boolean b) {
        hasEdit = b;
        return this;
    }

    public EditBar setLikeNum(int num) {
        if (badgeView == null) {
            badgeView = new MBadgeView(mContext);
        }
        badgeView.bindTarget(imgLike).setBadgeNumber(num);
        return this;
    }

    /**
     * 设置评论数
     */
    public EditBar setCommentNum(int num) {
        if (commentBadgeView == null) {
            commentBadgeView = new MBadgeView(mContext);
        }
        commentBadgeView.bindTarget(commentNum).setBadgeNumber(num);
        return this;
    }

    public EditBar setCollectNum(int num) {
        if (collectBadgeView == null) {
            collectBadgeView = new MBadgeView(mContext);
        }
        collectBadgeView.bindTarget(imgCollect).setBadgeNumber(num);
        return this;
    }

    /**
     * 是否隐藏评论
     */
    public EditBar hideComment(boolean isHide) {
        isHideComment = isHide;
        if (isHideComment) {
            commentNum.setVisibility(GONE);
        } else {
            commentNum.setVisibility(VISIBLE);
        }
        return this;
    }

    public EditBar hidePraise(boolean isHide) {
        isHidePraise = isHide;
        if (isHidePraise) {
            imgPraise.setVisibility(GONE);
        } else {
            imgPraise.setVisibility(VISIBLE);
        }
        return this;
    }

    public EditBar isPraise(boolean b) {
        isPraised = b;
        if (isPraised) {
            imgPraise.setImageResource(R.drawable.news_like_on_icon);
        } else {
            imgPraise.setImageResource(R.drawable.news_like_off_icon);
        }
        return this;
    }

    public EditBar setPraiseNum(int num) {
        if (praiseBadgeView == null) {
            praiseBadgeView = new MBadgeView(mContext);
        }
        praiseBadgeView.bindTarget(imgPraise).setBadgeNumber(num);
        return this;
    }

    public EditBar setShareNum(int num) {
        if (shareBadgeView == null) {
            shareBadgeView = new MBadgeView(mContext);
        }
        shareBadgeView.bindTarget(imgShare).setBadgeNumber(num);
        return this;
    }

    public EditBar hideText(boolean isHide) {
        isHideText = isHide;
        if (isHideText) {
            content_text.setVisibility(INVISIBLE);
        } else {
            content_text.setVisibility(VISIBLE);
        }
        return this;
    }

    /**
     * 是否隐藏分享
     */
    public EditBar hideShare(boolean isHide) {
        isHideShare = isHide;
        if (isHideShare) {
            imgShare.setVisibility(GONE);
        } else {
            imgShare.setVisibility(VISIBLE);
        }
        return this;
    }

    /**
     * 设置分享图标
     *
     * @param resId 资源id
     */
    public EditBar setShareIconRes(int resId) {
        imgShare.setImageResource(resId);
        return this;
    }

    /**
     * 设置收藏图标
     *
     * @param resId 资源id
     */
    public EditBar setCollectIconRes(int resId) {
        imgCollect.setImageResource(resId);
        return this;
    }


    /**
     * 是否隐藏收藏
     */
    public EditBar hideCollect(boolean isHide) {
        isHideCollect = isHide;
        if (isHideCollect) {
            imgCollect.setVisibility(GONE);
        } else {
            imgCollect.setVisibility(VISIBLE);
        }
        return this;
    }

    /**
     * 商品列表
     *
     * @param isShow 是否显示
     * @return
     */
    public EditBar showShop(boolean isShow) {
        isShowShop = isShow;
        if (isShowShop) {
            shop_btn.setVisibility(VISIBLE);
        } else {
            shop_btn.setVisibility(GONE);
        }
        return this;
    }


    public EditBar showLike(boolean isShow) {
        isShowLike = isShow;
        if (isShowLike) {
            imgLike.setVisibility(VISIBLE);
        } else {
            imgLike.setVisibility(GONE);
        }
        return this;
    }


    /**
     * 商品列表
     *
     * @param isShow 是否显示
     * @return
     */
    public EditBar showShop(boolean isShow, String msg) {
        isShowShop = isShow;
        shop_btn.setText(StringUtils.isEmpty(msg) ? "" : msg);
        if (isShowShop) {
            shop_btn.setVisibility(VISIBLE);
        } else {
            shop_btn.setVisibility(GONE);
        }
        return this;
    }


    public EditBar hideCommentLayout(boolean isHideCommentLayout) {
        isCommentLayoutHide = isHideCommentLayout;
        if (isCommentLayoutHide) {
            comment_layout.setVisibility(INVISIBLE);
        } else {
            comment_layout.setVisibility(VISIBLE);
        }
        return this;
    }

    /**
     * 设置是否已经收藏
     */
    public EditBar setCollect(boolean collect) {
        isCollect = collect;
        if (isCollect) {
            imgCollect.setImageResource(R.drawable.editbar_collect_on_press);
        } else {
            imgCollect.setImageResource(R.drawable.editbar_collect_press);
        }
        return this;
    }

    public EditBar setTextHint(String hint) {
        content_text.setHint(hint);
        return this;
    }

    public EditBar setEditTextHint(String hint) {
        contented.setHint(hint);
        return this;
    }

    /**
     * 设置背景颜色
     *
     * @param res
     * @return
     */
    public EditBar setBackground(int res) {
        rootLayout.setBackgroundResource(res);
        return this;
    }

    public boolean isCollect() {
        return isCollect;
    }

    public boolean isPraise() {
        return isPraised;
    }

    public String getContent() {
        return contented.getText().toString().trim();
    }

    public EditText getEditText() {
        return contented;
    }

    public void clearContent() {
        contented.setText("");
    }

    public int getType() {
        return type;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }
}
